#include "gpio.h"
#include "BK3432_reg.h"
#include "tm1621c_serial_drv.h"

static uint8_t wr_pin_num;
static uint8_t dat_pin_num;

#define WR_HIGH()		  gpio_set(wr_pin_num, 1)
#define WR_LOW()		  gpio_set(wr_pin_num, 0)
#define DAT_HIGH()		  gpio_set(dat_pin_num, 1)
#define DAT_LOW()		  gpio_set(dat_pin_num, 0)


/*static void delay(uint16_t dly_cnt)
{
	while(dly_cnt--);
}*/


void tm1621_tx_bits_msb(uint8_t dat, uint8_t bit_cnt)
{
	for(uint8_t i=0;i<bit_cnt;i++)
    {
		WR_LOW();
		if(i < 8)
		{
			if(dat&0x80)
			{
				DAT_HIGH();
			}
			else
			{
				DAT_LOW();
			}
			dat<<=1;
		}
		else
		{
			DAT_LOW();
		}
		WR_HIGH();
	}
}

void tm1621_tx_bits_lsb(uint8_t dat, uint8_t bit_cnt)
{
	for(uint8_t i=0;i<bit_cnt;i++)
    {
		WR_LOW();
		if(i < 8)
		{
			if(dat&0x01)
			{
				DAT_HIGH();
			}
			else
			{
				DAT_LOW();
			}
			dat>>=1;
		}
		else
		{
			DAT_LOW();
		}
		WR_HIGH();
	}
}


void tm1621_sw_pin_init(uint8_t wr_pin, uint8_t data_pin)
{
	wr_pin_num = wr_pin;
	dat_pin_num = data_pin;
    gpio_config(wr_pin_num, OUTPUT, PULL_NONE);
    gpio_config(dat_pin_num, OUTPUT, PULL_NONE);
}

